// pages/song/song.js
import {Song} from "./song-model.js";
var song=new Song();
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:-1,
    name:"",
    song:"",
    play:false,
    loop:true,
    audio:'',
    currentIndex:-1,
  },

  onLoad: function (options) {
   var name = options.name;
    if(options.from=="play"){
      app.globalData.back=true;
      var index=parseInt(options.index);
    }else{
      var id = parseInt(options.id);
      if(id){
        var index = this.getIndexByID(id);
      }else{
        var index=options.index;
      }
      
    }
   
    this.setData({
      index:index,
      name:name,
    });
    wx.setStorageSync("index", index);
    this.setLrc();
  },

  onShow:function(){
    var index=this.data.index;
    var name=this.data.name;
    var options={
      index:index,
      name:name
    };
    this.onLoad(options);
  },

  //判断当前歌曲是否在喜欢的列表中
  inLoveList:function(id){
    var loveList=wx.getStorageSync("loveList");
    var index=-1;
    if(loveList){
      for(let i=0;i<loveList.length;++i){
        if(id==loveList[i].id){
          return true;
        }
      }
      }
    return false;
  },
    
  getIndexByID:function(id){
    var songs = wx.getStorageSync("songs");
    for(let i=0;i<songs.length;++i){
      if(songs[i].id==id){
        return i;
      }
    }
  },

  setLrc:function(){
    var songs = wx.getStorageSync("songs");
    var songInfo = songs[this.data.index];
    var love=this.inLoveList(songInfo.id);
    songInfo.love=love;
    song.getLrc(songInfo.lrc, (res) => {
      this.setData({
        lrc: res,
        song: songInfo,
      });
    });
  },

  //监听播放形式
  onChange:function(e){
    if(this.data.loop){//循环播放

    }else{//随机播放

    }
    this.setData({
      loop: !this.data.loop
    })
   
  },

  //监听上一曲
  onPrev:function(e){
    var index = parseInt(this.data.index);
    if (index >= 0) {
      this.setData({
        index: index - 1
      });
      wx.setStorageSync("index", index - 1);
      this.setLrc();
      this.playByIndex(index - 1);
    }
    this.setCurrentIndex();
  },

  //监听播放
  onPlay:function(e){
    if(!this.data.audio){
      var audio = wx.createInnerAudioContext();
      var song = this.data.song;
      audio.src = song.url;
      this.data.audio = audio;
    }else{
      var audio = this.data.audio;
    }

    if (this.data.play) {
      audio.pause();
    } else {
      audio.play();
    }
    this.setData({
      play: !this.data.play,
    });
    this.setCurrentIndex();
  },

  setCurrentIndex:function(){
    var timeId=wx.getStorageSync("timeId");
    if(timeId){
      clearInterval(timeId);
    }
    if(this.data.lrc){
      var audio = this.data.audio;
      var duration = audio.duration;
      var timeArr = this.data.lrc.timeArr;
      var currentTime = -1;
      timeId = setInterval(() => {
        currentTime = audio.currentTime;
        var currentIndex = song.findCurrentIndex(currentTime, timeArr);
        this.setData({
          currentIndex: currentIndex
        });

      }, 2000);
      wx.setStorageSync("timeId", timeId);
    }
    
  },

  //监听下一曲
  onNext:function(e){
    var index=parseInt(this.data.index);
    if(index>=0){
      this.setData({
        index:index+1
      });
      wx.setStorageSync("index",index+1);
      this.setLrc();
      this.playByIndex(index+1);
    }
    this.setCurrentIndex();
  },

  //播放指定索引的歌曲
  playByIndex:function(index){
    var songs=wx.getStorageSync("songs");
    var audio=wx.createInnerAudioContext();
    this.data.audio.destroy();//销毁前一个对象
    audio.src=songs[index].url;
    this.setData({
      audio:audio,
      song:songs[index],
      audio:audio
    });
    audio.play();
    
  },

  onLove: function (e) {
    var love = this.data.song.love;
    var loveList = [];
    var song = this.data.song;
    if (wx.getStorageSync("loveList")) {
      loveList = wx.getStorageSync("loveList");
    }

    if (!love) {
      song.love=true;
      loveList.push(song);
    } else {
      loveList.forEach(function (item, index,arr) {
        if (item.id == song.id) {
          arr.splice(index,1);
          song.love = false;
        } 
      });
    }
    wx.setStorageSync("loveList", loveList);
    this.setData({
      song:song
    });
  },

  onUnload:function(){
    console.log("unload");
    var timeId=wx.getStorageSync("timeId");
    if(timeId){
      clearInterval(timeId);
      wx.removeStorageSync("timeId");
    }
    this.data.audio.destroy();
  }
})